import React from 'react';
import { Router, Route, IndexRedirect } from 'dva/router';

const cached = {};
function registerModel(app, model) {
  if (!cached[model.namespace]) {
    app.model(model);
    cached[model.namespace] = 1;
  }
}

function RouterConfig({ history, app }) {

  registerModel(app, require('./models/common/common'));

  function requireAuth(nextState, replace, callback) {

    app._store.dispatch({
      type: 'common/enterAuth',
      payload: {},
      onComplete: callback
    });
  }

  const routes = [
    {
      path: '/login',
      name: 'Login',
      getComponent(nextState, cb) {
        require.ensure([], (require) => {
          cb(null, require('./routes/Login'));
        });
      },
    },
    {
      path: '/',
      name: 'IndexPage',
      getComponent(nextState, cb) {
        require.ensure([], (require) => {
          registerModel(app, require('./models/index/index'));
          cb(null, require('./routes/IndexPage'));
        });
      },
      onEnter: requireAuth,
      childRoutes: [
        {
          path: '/sys/dept',
          name: 'Dept',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/sys/department'));
              cb(null, require('./routes/sys/Department.js'));
            });
          },
        },
        {
          path: '/sys/user',
          name: 'User',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/sys/user'));
              cb(null, require('./routes/sys/User.js'));
            });
          },
        },
        {
          path: '/sys/role',
          name: 'Role',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/sys/role'));
              cb(null, require('./routes/sys/Role.js'));
            });
          },
        }, {
          path: '/sys/log',
          name: 'Log',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/sys/log'));
              cb(null, require('./routes/sys/Log.js'));
            });
          },
        },
        {
          path: '/basic/assetType',
          name: 'AssetType',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/basic/assetType'));
              cb(null, require('./routes/basic/AssetType.js'));
            });
          },
        },
        {
          path: '/basic/assetAttr',
          name: 'AssetAttr',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/basic/assetAttr'));
              cb(null, require('./routes/basic/AssetAttr.js'));
            });
          },
        },
        {
          path: '/basic/assetBrand',
          name: 'AssetBrand',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/basic/assetBrand'));
              cb(null, require('./routes/basic/AssetBrand.js'));
            });
          },
        },
        {
          path: '/basic/assetModel',
          name: 'AssetModel',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/basic/assetModel'));
              cb(null, require('./routes/basic/AssetModel.js'));
            });
          },
        },
        {
          path: '/basic/dataCenter',
          name: 'DataCenter',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/basic/dataCenter'));
              cb(null, require('./routes/basic/DataCenter.js'));
            });
          },
        },
        {
          path: '/basic/tRoom',
          name: 'TRoom',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/basic/tRoom'));
              cb(null, require('./routes/basic/TRoom.js'));
            });
          },
        },
        {
          path: '/basic/storeRoom',
          name: 'StoreRoom',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/basic/storeRoom'));
              cb(null, require('./routes/basic/StoreRoom.js'));
            });
          },
        },
        {
          path: '/equipment/preset',
          name: 'Preset',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/equipment/preset'));
              cb(null, require('./routes/equipment/Preset.js'));
            });
          },
        },
        {
          path: '/equipment/arrival',
          name: 'Arrival',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/equipment/arrival'));
              cb(null, require('./routes/equipment/Arrival.js'));
            });
          },
        },
        {
          path: '/instock',
          name: 'Instock',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/shelf/instock'));
              cb(null, require('./routes/shelf/Instock.js'));
            });
          },
        },
        {
          path: '/using/apply',
          name: 'Apply',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/using/apply'));
              cb(null, require('./routes/using/Apply.js'));
            });
          },
        },
        {
          path: '/using/distribution',
          name: 'Distribution',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/using/distribution'));
              cb(null, require('./routes/using/Distribution.js'));
            });
          },
        },
        {
          path: '/shelf/up',
          name: 'ShelfUp',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/shelf/shelfUp'));
              cb(null, require('./routes/shelf/ShelfUp.js'));
            });
          },
        },
        {
          path: '/shelf/down',
          name: 'ShelfDown',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/shelf/shelfDown'));
              cb(null, require('./routes/shelf/ShelfDown.js'));
            });
          },
        },
        {
          path: '/transfer',
          name: 'Transfer',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/transfer/transfer'));
              cb(null, require('./routes/transfer/Transfer.js'));
            });
          },
        }, {
          path: '/borrow/borrow',
          name: 'Borrow',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/borrow/borrow'));
              cb(null, require('./routes/borrow/Borrow.js'));
            });
          },
        }, {
          path: '/repair',
          name: 'Repair',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/repair/repair'));
              cb(null, require('./routes/repair/Repair.js'));
            });
          },
        },
        {
          path: '/scrap',
          name: 'Scrap',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/scrap/scrap'));
              cb(null, require('./routes/scrap/Scrap.js'));
            });
          },
        },
        {
          path: '/asset/maintain',
          name: 'Maintain',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/asset/maintain'));
              cb(null, require('./routes/asset/Maintain.js'));
            });
          },
        },
        {
          path: '/ledger/assetTypeLedger',
          name: 'AssetTypeLedger',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/ledger/assetTypeLedger'));
              cb(null, require('./routes/ledger/AssetTypeLedger.js'));
            });
          },
        },
        {
          path: '/ledger/assetStatus',
          name: 'AssetStatus',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/ledger/assetStatus'));
              cb(null, require('./routes/ledger/AssetStatus.js'));
            });
          },
        },
        {
          path: '/ledger/assetBrandLedger',
          name: 'AssetBrandLedger',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/ledger/assetBrandLedger'));
              cb(null, require('./routes/ledger/AssetBrandLedger.js'));
            });
          },
          onEnter: requireAuth,
        },
        {
          path: '/ledger/assetDepart',
          name: 'AssetDepart',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/ledger/assetDepart'));
              cb(null, require('./routes/ledger/AssetDepart.js'));
            });
          },
        },
        {
          path: '/ledger/serviceLifeLedger',
          name: 'serviceLifeLedger',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/ledger/serviceLifeLedger'));
              cb(null, require('./routes/ledger/ServiceLifeLedger.js'));
            });
          },
        },{
          path: '/ledger/cabinetCount',
          name: 'cabinetCount',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/ledger/cabinetCount'));
              cb(null, require('./routes/ledger/CabinetCount.js'));
            });
          },
        },
        {
          path: '/inventory',
          name: 'Inventory',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/inventory/inventory'));
              cb(null, require('./routes/inventory/Inventory.js'));
            });
          },
        },
        {
          path: '/reportPlan',
          name: 'reportPlan',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/report/reportPlan'));
              cb(null, require('./routes/report/ReportPlan.js'));
            });
          },
        },
        {
          path: '/reportList',
          name: 'reportList',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/report/reportList'));
              cb(null, require('./routes/report/ReportList.js'));
            });
          },
        },
        {
          path: '/inventorySheet',
          name: 'InventorySheet',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/inventory/inventorySheet'));
              cb(null, require('./routes/inventory/InventorySheet.js'));
            });
          },
        },
        {
          path: '/patrolPlan',
          name: 'patrolPlan',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/patrol/patrolPlan'));
              cb(null, require('./routes/patrol/PatrolPlan.js'));
            });
          },
        },
        {
          path: '/hard/hardware',
          name: 'Hardware',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/hard/hardware'));
              cb(null, require('./routes/hard/Hardware.js'));
            });
          },
        },
        {
          path: '/out',
          name: 'Out',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/asset/out'));
              cb(null, require('./routes/asset/Out.js'));
            });
          },
        },
        {
          path: '/assetRecord',
          name: 'assetRecord',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/asset/assetRecord'));
              cb(null, require('./routes/asset/AssetRecord.js'));
            });
          },
        },
        {
          path: '/OutOfStorage',
          name: 'OutOfStorage',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/asset/outOfStorage'));
              cb(null, require('./routes/asset/OutOfStorage.js'));
            });
          },
        },
        {
          path: '/cabinetGroup',
          name: 'cabinetGroup',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/cabinet/cabinetGroup'));
              cb(null, require('./routes/cabinet/CabinetGroup.js'));
            });
          },
        },
        {
          path: '/cabinet',
          name: 'cabinet',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/cabinet/cabinet'));
              cb(null, require('./routes/cabinet/Cabinet.js'));
            });
          },
        },
        {
          path: '/patrol',
          name: 'patrol',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/patrol/patrol'));
              cb(null, require('./routes/patrol/Patrol.js'));
            });
          },
        },
        {
          path: '/virtual',
          name: 'virtual',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/virtual/virtual'));
              cb(null, require('./routes/virtual/Virtual.js'));
            });
          },
        },
        {
          path: '/alarm',
          name: 'Alarm',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/alarm/alarm'));
              cb(null, require('./routes/alarm/Alarm.js'));
            });
          },
        },
        {
          path: '/topology',
          name: 'Topology',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/topology/topology'));
              cb(null, require('./routes/topology/Topology.js'));
            });
          },
        },
        {
          path: '/contract',
          name: 'Contract',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/contract/contract'));
              cb(null, require('./routes/contract/Contract.js'));
            });
          },
        },
        {
          path: '/visitor',
          name: 'Visitor',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/visitor/visitor'));
              cb(null, require('./routes/visitor/Registration.js'));
            });
          },
        },
        {
          path: '/ipParam',
          name: 'ipParam',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/resource/ipParam'));
              cb(null, require('./routes/resource/IpParam.js'));
            });
          },
        },

        {
          path: '/resourceGroup',
          name: 'resourceGroup',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/resource/resourceGroup'));
              cb(null, require('./routes/resource/ResourceGroup.js'));
            });
          },
        },
        {
          path: '/resource',
          name: 'resource',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/resource/resource'));
              cb(null, require('./routes/resource/Resource.js'));
            });
          },
        },
        {
          path: '/maintenant',
          name: 'maintenant',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/alarm/maintenant'));
              cb(null, require('./routes/alarm/Maintenant.js'));
            });
          },
        },
        {
          path: '/asset/dynamicSearch',
          name: 'dynamicSearch',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/asset/dynamicSearch'));
              cb(null, require('./routes/asset/DynamicSearch.js'));
            });
          },
        },
         {
          path: '/temperature',
          name: 'temperature',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/cabinet/temperature'));
              cb(null, require('./routes/cabinet/Temperature.js'));
            });
          },
        },
        {
         path: '/space/space',
         name: 'space',
         getComponent(nextState, cb) {
           require.ensure([], (require) => {
             registerModel(app, require('./models/space/space'));
             cb(null, require('./routes/space/Space.js'));
           });
         },
       },
      ]
    },
  ];

  return <Router history={history} routes={routes} ></Router>;
}


export default RouterConfig;
